<?php
/**
 * Created by PhpStorm.
 * User: Wu Ning
 * Date: 2018/5/20
 * Time: 16:20
 */
namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Model\Backstage\RoomFollow;
use App\Model\Backstage\CustomerFollowSourde;
use App\Model\Backstage\RoomInfo;
use App\Tool\Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class   RoomFollowController extends Controller
{
    /*
     * 添加房源跟进
     * Wu Ning
     */
    public function addRoomFollow(Request $request,RoomFollow $RoomFollow,CustomerFollowSourde $customerfollowsourde, RoomInfo $roomInfo)
    {
        $params = $request->all();
        $validator = Validator::make($params, [
            'f_room_id'         => 'required',
            'f_follow_up_way'   => 'required',
            'f_content'      => 'required',
        ]);
        if ($validator->fails()) {
            return $this->error(Error::PARAM_MISSING);
        }
        //开启事务
        DB::beginTransaction();
        $res = $RoomFollow->addRoomFollow($params,$customerId = '');
        //添加提醒跟进范围  提醒时间有的话   添加提醒范围
        if (isset($params['cfs_remind_time'])){
            $cfs_user_id = explode(',', $params['cfs_user_id']);
            $cfsid = $customerfollowsourde->addCustomerFollowSourde($params['cfs_type_id'], $params['cfs_type'], $cfs_user_id, $params['cfs_remind_time'], $params['cfs_content']);
        }else {
            $cfsid = true;
        }
        //更新房源最后跟进时间
        $saveTime = $roomInfo->updateFollowTime($params['f_room_id']);
        if($res && $cfsid && $saveTime){
            DB::commit();
            return $this->success('ok', '添加成功');
        } else {
            DB::rollback();
            return $this->error(Error::ADD_ERROR);
        }
    }


}